package com.santiao.fuel.utils.UnitConverter;

import java.math.BigDecimal;
import java.math.RoundingMode;


/**
 * 重量单位转换 工具类
 */
public class WeightUnitConverter {

    private static final BigDecimal KILOGRAM_TO_GRAM = new BigDecimal("1000");
    private static final BigDecimal GRAM_TO_MILLIGRAM = new BigDecimal("1000");
    private static final BigDecimal MILLIGRAM_TO_MICROGRAM = new BigDecimal("1000");
    private static final BigDecimal KILOGRAM_TO_QUINTAL = new BigDecimal("0.01");
    private static final BigDecimal KILOGRAM_TO_CARAT = new BigDecimal("5000");
    private static final BigDecimal KILOGRAM_TO_JIN = new BigDecimal("2");
    private static final BigDecimal KILOGRAM_TO_DAN = new BigDecimal("0.002");
    private static final BigDecimal KILOGRAM_TO_LIANG = new BigDecimal("20");
    private static final BigDecimal KILOGRAM_TO_QIAN = new BigDecimal("200");
    private static final BigDecimal KILOGRAM_TO_TROY_POUND = new BigDecimal("2.679228880719");
    private static final BigDecimal KILOGRAM_TO_TROY_OUNCE = new BigDecimal("32.150746568628");
    private static final BigDecimal KILOGRAM_TO_PENNYWEIGHT = new BigDecimal("643.01493137256");
    private static final BigDecimal KILOGRAM_TO_TROY_GRAIN = new BigDecimal("15432.358352941");
    private static final BigDecimal KILOGRAM_TO_LONG_TON = new BigDecimal("0.0009842065276");
    private static final BigDecimal KILOGRAM_TO_SHORT_TON = new BigDecimal("0.001102311311");
    private static final BigDecimal KILOGRAM_TO_UK_HUNDREDWEIGHT = new BigDecimal("0.019684130552");
    private static final BigDecimal KILOGRAM_TO_US_HUNDREDWEIGHT = new BigDecimal("0.02204622622");
    private static final BigDecimal KILOGRAM_TO_STONE = new BigDecimal("0.15747304442");
    private static final BigDecimal KILOGRAM_TO_OUNCE = new BigDecimal("35.27396194958");
    private static final BigDecimal KILOGRAM_TO_DRAM = new BigDecimal("564.38339119329");
    private static final BigDecimal KILOGRAM_TO_GRAIN = new BigDecimal("15432.358352941");

    public static BigDecimal convert(String fromUnit, String toUnit, BigDecimal value) {
        BigDecimal baseValue = toBaseUnit(fromUnit, value);
        return fromBaseUnit(toUnit, baseValue).setScale(2,RoundingMode.HALF_UP);
    }

    private static BigDecimal toBaseUnit(String unit, BigDecimal value) {
        switch (unit) {
            case "克":
                return value.divide(KILOGRAM_TO_GRAM, 10, RoundingMode.HALF_UP);
            case "毫克":
                return value.divide(KILOGRAM_TO_GRAM.multiply(GRAM_TO_MILLIGRAM), 10, RoundingMode.HALF_UP);
            case "微克":
                return value.divide(KILOGRAM_TO_GRAM.multiply(GRAM_TO_MILLIGRAM).multiply(MILLIGRAM_TO_MICROGRAM), 10, RoundingMode.HALF_UP);
            case "公担":
                return value.divide(KILOGRAM_TO_QUINTAL, 10, RoundingMode.HALF_UP);
            case "克拉":
                return value.divide(KILOGRAM_TO_CARAT, 10, RoundingMode.HALF_UP);
            case "斤/市斤":
                return value.divide(KILOGRAM_TO_JIN, 10, RoundingMode.HALF_UP);
            case "担":
                return value.divide(KILOGRAM_TO_DAN, 10, RoundingMode.HALF_UP);
            case "两":
                return value.divide(KILOGRAM_TO_LIANG, 10, RoundingMode.HALF_UP);
            case "钱":
                return value.divide(KILOGRAM_TO_QIAN, 10, RoundingMode.HALF_UP);
            case "金衡磅":
                return value.divide(KILOGRAM_TO_TROY_POUND, 10, RoundingMode.HALF_UP);
            case "金衡盎司":
                return value.divide(KILOGRAM_TO_TROY_OUNCE, 10, RoundingMode.HALF_UP);
            case "英钱":
                return value.divide(KILOGRAM_TO_PENNYWEIGHT, 10, RoundingMode.HALF_UP);
            case "金衡格令":
                return value.divide(KILOGRAM_TO_TROY_GRAIN, 10, RoundingMode.HALF_UP);
            case "英制长吨":
                return value.divide(KILOGRAM_TO_LONG_TON, 10, RoundingMode.HALF_UP);
            case "英制短吨":
                return value.divide(KILOGRAM_TO_SHORT_TON, 10, RoundingMode.HALF_UP);
            case "英担":
                return value.divide(KILOGRAM_TO_UK_HUNDREDWEIGHT, 10, RoundingMode.HALF_UP);
            case "美担":
                return value.divide(KILOGRAM_TO_US_HUNDREDWEIGHT, 10, RoundingMode.HALF_UP);
            case "英石":
                return value.divide(KILOGRAM_TO_STONE, 10, RoundingMode.HALF_UP);
            case "盎司":
                return value.divide(KILOGRAM_TO_OUNCE, 10, RoundingMode.HALF_UP);
            case "打兰":
                return value.divide(KILOGRAM_TO_DRAM, 10, RoundingMode.HALF_UP);
            case "格令":
                return value.divide(KILOGRAM_TO_GRAIN, 10, RoundingMode.HALF_UP);
            default:
                return value; // 默认为公斤
        }
    }

    private static BigDecimal fromBaseUnit(String unit, BigDecimal value) {
        switch (unit) {
            case "克":
                return value.multiply(KILOGRAM_TO_GRAM);
            case "毫克":
                return value.multiply(KILOGRAM_TO_GRAM.multiply(GRAM_TO_MILLIGRAM));
            case "微克":
                return value.multiply(KILOGRAM_TO_GRAM.multiply(GRAM_TO_MILLIGRAM).multiply(MILLIGRAM_TO_MICROGRAM));
            case "公担":
                return value.multiply(KILOGRAM_TO_QUINTAL);
            case "克拉":
                return value.multiply(KILOGRAM_TO_CARAT);
            case "斤/市斤":
                return value.multiply(KILOGRAM_TO_JIN);
            case "担":
                return value.multiply(KILOGRAM_TO_DAN);
            case "两":
                return value.multiply(KILOGRAM_TO_LIANG);
            case "钱":
                return value.multiply(KILOGRAM_TO_QIAN);
            case "金衡磅":
                return value.multiply(KILOGRAM_TO_TROY_POUND);
            case "金衡盎司":
                return value.multiply(KILOGRAM_TO_TROY_OUNCE);
            case "英钱":
                return value.multiply(KILOGRAM_TO_PENNYWEIGHT);
            case "金衡格令":
                return value.multiply(KILOGRAM_TO_TROY_GRAIN);
            case "英制长吨":
                return value.multiply(KILOGRAM_TO_LONG_TON);
            case "英制短吨":
                return value.multiply(KILOGRAM_TO_SHORT_TON);
            case "英担":
                return value.multiply(KILOGRAM_TO_UK_HUNDREDWEIGHT);
            case "美担":
                return value.multiply(KILOGRAM_TO_US_HUNDREDWEIGHT);
            case "英石":
                return value.multiply(KILOGRAM_TO_STONE);
            case "盎司":
                return value.multiply(KILOGRAM_TO_OUNCE);
            case "打兰":
                return value.multiply(KILOGRAM_TO_DRAM);
            case "格令":
                return value.multiply(KILOGRAM_TO_GRAIN);
            default:
                return value; // 默认为公斤
        }

    }

}
